<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/GracePeriod.class.php";

/**
 * Manages grace periods, used to delete
 * characters that are too old.
 * 
 * @author Emmanuel Cron
 *
 */
class GracePeriodManager {
	/** The single instance of this manager. */
	static private $instance;
	
	private $grace_periods;
	
	/**
	 * Returns an instance of this class or
	 * creates a new one if none exists.
	 * 
	 * @return an instance of this class
	 */
	static function GetInstance() {
		if (empty(self::$instance)) {
			self::$instance = new GracePeriodManager();
		}
		
		return self::$instance;
	}
	
	/**
	 * Creates a new instance of this class,
	 * initializing the available grace periods
	 * by reading the associated file containing
	 * serialized objects.
	 */
	private function __construct() {
		$this->grace_periods = unserialize(file_get_contents(dirname(__FILE__)."/../class.data/GracePeriodManager.grace_periods.serialize"));
	}
	
	/**
	 * Returns all the grace periods configured
	 * on this roster.
	 * 
	 * @return an array containing GracePeriod
	 * 		objects
	 */
	function GetGracePeriods() {
		return $this->grace_periods;
	}
	
	/**
	 * Returns the grace period with the given
	 * id or a default one if none exists.
	 * 
	 * @param $id the id of the grace period to
	 * 		return
	 * @return the grace period corresponding to
	 * 		the given id, or a default one if
	 * 		none exists
	 */
	function GetGracePeriodById($id) {
		foreach($this->grace_periods as $grace_period) {
			if ($grace_period->GetId() == $id) {
				return $grace_period;
			} 
		}
		
		return $this->grace_periods[0];
	}
}

?>